package cn.csc.module.order.controller.admin.returns;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.order.controller.admin.returns.vo.*;
import cn.csc.module.order.convert.returns.OrderReturnsConvert;
import cn.csc.module.order.dal.dataobject.returns.OrderReturnsDO;
import cn.csc.module.order.service.returns.OrderReturnsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 订单退换货")
@RestController
@RequestMapping("/order/returns")
@Validated
public class OrderReturnsController {

    @Resource
    private OrderReturnsService returnsService;

    @PostMapping("/create")
    @ApiOperation("创建订单退换货")
    @PreAuthorize("@ss.hasPermission('order:returns:create')")
    public CommonResult<BigInteger> createReturns(@Valid @RequestBody OrderReturnsCreateReqVO createReqVO) {
        return success(returnsService.createReturns(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新订单退换货")
    @PreAuthorize("@ss.hasPermission('order:returns:update')")
    public CommonResult<Boolean> updateReturns(@Valid @RequestBody OrderReturnsUpdateReqVO updateReqVO) {
        returnsService.updateReturns(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除订单退换货")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:returns:delete')")
    public CommonResult<Boolean> deleteReturns(@RequestParam("id") BigInteger id) {
        returnsService.deleteReturns(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得订单退换货")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:returns:query')")
    public CommonResult<OrderReturnsRespVO> getReturns(@RequestParam("id") BigInteger id) {
        OrderReturnsDO returns = returnsService.getReturns(id);
        return success(OrderReturnsConvert.INSTANCE.convert(returns));
    }

    @GetMapping("/list")
    @ApiOperation("获得订单退换货列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('order:returns:query')")
    public CommonResult<List<OrderReturnsRespVO>> getReturnsList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<OrderReturnsDO> list = returnsService.getReturnsList(ids);
        return success(OrderReturnsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得订单退换货分页")
    @PreAuthorize("@ss.hasPermission('order:returns:query')")
    public CommonResult<PageResult<OrderReturnsRespVO>> getReturnsPage(@Valid OrderReturnsPageReqVO pageVO) {
        PageResult<OrderReturnsDO> pageResult = returnsService.getReturnsPage(pageVO);
        return success(OrderReturnsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出订单退换货 Excel")
    @PreAuthorize("@ss.hasPermission('order:returns:export')")
    @OperateLog(type = EXPORT)
    public void exportReturnsExcel(@Valid OrderReturnsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderReturnsDO> list = returnsService.getReturnsList(exportReqVO);
        // 导出 Excel
        List<OrderReturnsExcelVO> datas = OrderReturnsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "订单退换货.xls", "数据", OrderReturnsExcelVO.class, datas);
    }

}
